import unittest

import requests

from api.OpenAccountApi import OpenAccountApi

from parameterized import parameterized

from api.RegLoginApi import RegLoginApi
from common.resd_json import read_json
from common.utils_assert import common_assert
from common.read_html_util import html_util


class TestOpenAccount(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        # 实例化封装接口请求的类
        self.ses = requests.session()
        self.reg_login_api = RegLoginApi(self.ses)
        self.open_account_api = OpenAccountApi(self.ses)

    def tearDown(self) -> None:
        # 关闭session对象
        self.ses.close()

    # 实名认证测试用例
    @parameterized.expand(read_json("test02_data", "api1_data"))
    def test01_realname(self, keywords, password, realname, card_id, expect_status_code, expect_status,
                        expect_description, expect_card_id, expect_realname):
        # 实现登录接口
        # 实现实名认证接口
        # 断言
        self.reg_login_api.user_login(keywords, password)
        resp = self.open_account_api.realname(realname, card_id)
        common_assert(self, resp, expect_status_code, expect_status, expect_description)
        if expect_status == 200:
            self.assertEqual(expect_card_id, resp.json().get("data").get("card_id"))
            self.assertEqual(expect_realname, resp.json().get("data").get("realname"))

    # 开户测试用例
    def test02_open_account(self):
        # 实现登录接口
        # 实现开户接口
        # 断言
        self.reg_login_api.user_login("18373444511", "11111MM")
        resp = self.open_account_api.open_account()
        self.assertEqual(200, resp.status_code)
        self.assertEqual(200, resp.json()["status"])
        self.assertIn("form", resp.json()["description"])

    # 第三方开户测试用例
    def test03_third_open_account(self):
        # 实现登录接口
        # 实现开户接口
        # 实现第三方开户接口
        # 断言
        self.reg_login_api.user_login("18373444511", "11111MM")
        data = self.open_account_api.open_account()
        print(data,type(data))
        url=html_util(data)[0]
        form_dict=html_util(data)[1]
        resp=self.open_account_api.third_open_account(url,form_dict)
        self.assertEqual(200,resp.status_code)
        self.assertIn("OK",resp.text)



